package vocabularyhub.io.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import vocabularyhub.io.model.ISimplestReader;
import vocabularyhub.word.model.IWord;

/**
 * doing
 * 
 * @author Leon
 * 
 */
public class Reader implements ISimplestReader {
    static Logger logger = Logger.getLogger(Reader.class);
    public static final String word_phonetic_trans = "(\\w+)\\s+(\\[.*])\\s+(.*)";

    @Override
    public List<IWord> parseWordListFile(File f) throws FileNotFoundException {

        Pattern p = Pattern.compile(word_phonetic_trans);
        Matcher m = null;
        BufferedReader reader = new BufferedReader(new FileReader(f));
        String line;
        try {
            @SuppressWarnings("unused")
            String word = null, phonetic = null, trans = null;
            while ((line = reader.readLine()) != null) {
                m = p.matcher(line);
                if (m.find() == false) {
                    continue;
                }
                try {
                    word = m.group(1);
                    phonetic = m.group(2);
                    trans = m.group(3);

                    // SimplestWord w = new SimplestWord();
                    // w.setWord(word);

                } catch (IllegalStateException e) {
                    System.out.println(e.getMessage());
                }

            }
        } catch (IOException e) {
            logger.warn(e.getMessage());
        } catch (Exception e) {
            logger.warn(e.getMessage());
        }

        return null;
    }

}
